package com.anecdote.ideaplugins.changesbar;

/**
 * Copyright 2006 Nathan Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.intellij.idea.ActionsBundle;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.LocalChangeList;
import com.intellij.openapi.vcs.changes.ui.NewChangelistDialog;

class NewChangeListAction extends AnAction
{
  private final ChangesBar _changesBar;

  NewChangeListAction(ChangesBar changesBar)
  {
    //noinspection UnresolvedPropertyKey
    super(ActionsBundle.message("action.ChangesView.NewChangeList.text") + "...",
          ActionsBundle.message("action.ChangesView.NewChangeList.description"), IconLoader.getIcon(
      "/general/add.png"));
    if (getTemplatePresentation().getText().startsWith("!")) {
      getTemplatePresentation().setText("New Changelist...");
    }
    _changesBar = changesBar;
  }

  public void actionPerformed(AnActionEvent e)
  {
    final NewChangelistDialog dialogWrapper = new NewChangelistDialog(_changesBar.getProject());
    dialogWrapper.show();
    if ((dialogWrapper.getExitCode() != DialogWrapper.OK_EXIT_CODE || dialogWrapper.getName().length() <= 0)) {
      return;
    }
    final ChangeListManager changeListManager = ChangeListManager.getInstance(_changesBar.getProject());
    final LocalChangeList list = changeListManager.addChangeList(dialogWrapper.getName(),
                                                                 dialogWrapper.getDescription());
    changeListManager.setDefaultChangeList(list);
  }
}
